package com.shahe.GirlsDay;

import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTabHost;
import android.support.v4.app.FragmentTransaction;
import android.view.View;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;
import com.shahe.GirlsDay.fragment.*;

public class LogActivity extends FragmentActivity implements OnCheckedChangeListener {

    private static final String TAG = LogActivity.class.getSimpleName();
    private FragmentTabHost mTabHost;
    private RadioGroup radioGroup;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.log);

        findView();
    }

    private void findView() {
        mTabHost = (FragmentTabHost) findViewById(android.R.id.tabhost);
        mTabHost.setup(this, getSupportFragmentManager(), R.id.realtabcontent);
        mTabHost.getTabWidget().setVisibility(View.GONE);	//����ϵͳ��TabWidget

        mTabHost.addTab(mTabHost.newTabSpec("basic").setIndicator("Basic"),
                BasicFragment.class, null);

        mTabHost.addTab(mTabHost.newTabSpec("symptom").setIndicator("Symptom"),
                SymptomFragment.class, null);
        mTabHost.addTab(mTabHost.newTabSpec("event").setIndicator("Event"),
                EventFragment.class, null);

        mTabHost.addTab(mTabHost.newTabSpec("note").setIndicator("Note"),
                NoteFragment.class, null);



//		mTabHost.setOnTabChangedListener(this);
        mTabHost.setCurrentTabByTag("basic");
        ((RadioButton) findViewById(R.id.radio_basic)).setChecked(true);

        radioGroup = (RadioGroup) findViewById(R.id.radiogroup);
        radioGroup.setOnCheckedChangeListener(this);
    }

    @Override
    public void onCheckedChanged(RadioGroup group, int checkedId) {
        FragmentManager fm = getSupportFragmentManager();
        BasicFragment basicFragment = (BasicFragment) fm.findFragmentByTag("basic");
        SymptomFragment symptomFragment = (SymptomFragment) fm
                .findFragmentByTag("symptom");
        EventFragment eventFragment = (EventFragment) fm
                .findFragmentByTag("event");
        NoteFragment noteFragment = (NoteFragment) fm
                .findFragmentByTag("note");


        FragmentTransaction ft = fm.beginTransaction();

        //** Detaches the androidfragment if exists */
        if (basicFragment != null)
            ft.detach(basicFragment);

        if (symptomFragment != null)
            ft.detach(symptomFragment);

        if (eventFragment != null)
            ft.detach(eventFragment);

        if (noteFragment != null)
            ft.detach(noteFragment);

        //if (moreFragment != null)
        //ft.detach(moreFragment);


        switch (checkedId) {
            case R.id.radio_basic:
                if (basicFragment == null) {
                    /** Create AndroidFragment and adding to fragmenttransaction */
                    ft.add(R.id.realtabcontent, new BasicFragment(), "basic");
                } else {
                    /** Bring to the front, if already exists in the fragmenttransaction */
                    ft.attach(basicFragment);
                }
                mTabHost.setCurrentTabByTag("basic");
                break;
            case R.id.radio_symptom:
                if (symptomFragment == null) {
                    ft.add(R.id.realtabcontent, new SymptomFragment(), "symptom");
                } else {
                    ft.attach(symptomFragment);
                }
                mTabHost.setCurrentTabByTag("symptom");
                break;
            case R.id.radio_event:
                if (eventFragment == null) {
                    ft.add(R.id.realtabcontent, new EventFragment(), "event");
                } else {
                    ft.attach(eventFragment);
                }
                mTabHost.setCurrentTabByTag("event");
                break;
            case R.id.radio_note:
                if (noteFragment == null) {
                    ft.add(R.id.realtabcontent, new NoteFragment(), "note");
                } else {
                    ft.attach(noteFragment);
                }
                mTabHost.setCurrentTabByTag("note");
                break;

        }
    }
}
